package view
{
	import flash.events.Event;
	import flash.text.TextField;
	
	import mx.core.UIComponent;

	public class LineIcon extends UIComponent
	{
		private const COLOR:uint = 0x000000;

	    private var elementIcon1:ElementIcon;
	    private var elementIcon2:ElementIcon;
	    
	    private var textField:TextField = new TextField(); // 関係線上に表示する文字列
    
    	public function LineIcon(ei1:ElementIcon, ei2:ElementIcon)
		{
			super();

			// 内部コンポーネントを初期化します
			this.textField = new TextField();
			this.textField.text = "初期値";
			this.textField.backgroundColor = 0xFFFFFF;
			this.textField.selectable = false;
	        this.addChild(textField);

			// 関係線に要素を接続する			
			this.elementIcon1 = ei1;
			this.elementIcon2 = ei2;
			this.elementIcon1.addLine(this);
			this.elementIcon2.addLine(this);
			
			// イベントハンドラを設定する
	        addEventListener(Event.RENDER, onRender);

			// 要素間を結ぶようサイズを調整する
			calculateSize();
	    }

	    private function onRender(event:Event):void {
			draw();
	    }
	    
	    private function draw():void {
			this.graphics.lineStyle(0,COLOR);
			drawLine();
    	}
    	
    	private function clear():void {
			this.graphics.clear();
    	}
    	
    	private function drawLine():void {
	    	this.graphics.moveTo(0,0);
	    	this.graphics.lineTo(this.width, this.height);
	    }
    
    	private function calculateSize():void {
    		// 直線を表示する領域のサイズを計算します
			this.x = this.elementIcon1.centerX;
			this.y = this.elementIcon1.centerY;
			this.width = this.elementIcon2.centerX - this.elementIcon1.centerX;
			this.height = this.elementIcon2.centerY - this.elementIcon1.centerY;
			
			// 文字列を表示する位置を計算します
			this.textField.x = (this.width / 2) - (this.textField.textWidth / 2);
			this.textField.y = (this.height / 2) - (this.textField.textHeight / 2);
    	}		
    	
    	public function redraw():void {
    		clear();
    		calculateSize();
    		draw();
    	}
    	
    	public function set text(_text:String):void {
    		this.textField.text = _text;
    	}
	}
}